package com.example.booksystem.action;

import com.example.booksystem.bean.Type;
import com.example.booksystem.biz.TypeBiz;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;


@WebServlet("/type")
public class TypeServlet extends HttpServlet {
    TypeBiz typeBiz = new TypeBiz();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //获取对象
        PrintWriter out = resp.getWriter();
        ServletContext application = req.getServletContext();

        //完成业务
        String type = req.getParameter("type");
        switch (type){
            case "add":
                add(req,resp,out,application);
                break;
            case "modifypre":
                modifyPre(req,resp,out,application);
                break;
            case "modify":
                modify(req,resp,out,application);
                break;
            case "remove":
                remove(req,resp,out,application);
                break;

        }
    }

    private void remove(HttpServletRequest req, HttpServletResponse resp, PrintWriter out, ServletContext application) {
        long id = Long.parseLong(req.getParameter("id"));
        try {
            int remove = typeBiz.remove(id);
            if (remove>0){
                List<Type> all = typeBiz.getAll();
                application.setAttribute("types",all);
                out.println("<script>alert('删除成功');location.href='type_list.jsp';</script>");
            }else {
                out.println("<script>alert('删除失败');location.href='type_list.jsp';</script>");
            }
        } catch (Exception e) {
            out.println("<script>alert('"+e.getMessage()+"');location.href='type_list.jsp';</script>");
        }
    }

    private void modify(HttpServletRequest req, HttpServletResponse resp, PrintWriter out, ServletContext application) {
        long id = Long.parseLong(req.getParameter("typeId"));
        String typeName = req.getParameter("typeName");
        long parentType = Long.parseLong(req.getParameter("parentType"));

        int result = typeBiz.modify(id, typeName, parentType);
        if (result>0){
            List<Type> types = typeBiz.getAll();
            application.setAttribute("types",types);
            out.println("<script>alert('修改成功');location.href='type_list.jsp';</script>");
        }else {
            out.println("<script>alert('修改失败');location.href='type_list.jsp';</script>");
        }

    }

    private void modifyPre(HttpServletRequest req, HttpServletResponse resp, PrintWriter out, ServletContext application) throws ServletException, IOException {
        long id = Long.parseLong(req.getParameter("id"));
        Type type = typeBiz.getById(id);
        req.setAttribute("type", type);
        req.getRequestDispatcher("type_modify.jsp").forward(req,resp);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp, PrintWriter out, ServletContext application) {
        String typeName = req.getParameter("typeName");
        String parent = req.getParameter("parentType")==null ? "0" : req.getParameter("parentType");
        long parentType = Long.parseLong(parent);
        int result = typeBiz.add(typeName, parentType);
        if (result>0){
            List<Type> types = typeBiz.getAll();
            application.setAttribute("types",types);
            out.println("<script>alert('添加成功');location.href='type_list.jsp';</script>");
        }else {
            out.println("<script>alert('添加失败');location.href='type_add.jsp';</script>");
        }

    }
}
